<template>
    <div>
        <div class="note"> 
            <p><i>提示：1.当前搜索只能搜索本科室所对应的信息，体检前请核对用户信息是否准确</i></p>
            <p style="text-indent: 3em;"><i>2.如若提示"暂无该用户"，请检查输入搜索信息是否准确/用户订单是都正确</i></p>
        </div>
        <div style="margin-top: 15px;">
            <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
            <el-select v-model="select" slot="prepend" placeholder="请选择">
                <el-option label="条形编码" value = 1></el-option>
                <el-option label="身份证信息" value= 2></el-option>
            </el-select>
            <el-button @click="searchBtn" slot="append" icon="el-icon-search"></el-button>
            </el-input>
            <hr class="hr-solid-content" data-content="项目">            
            <el-table
                :data="tableData1"
                border
                style="width: 100%">
                <el-table-column
                    prop="department"
                    label="所属科室"
                    width="80">
                </el-table-column>
                <el-table-column
                    prop="proName"
                    label="项目名"
                    width="150">
                </el-table-column>
                <el-table-column
                    prop="patientName"
                    label="姓名"
                    width="130">
                </el-table-column>
                <el-table-column
                    prop="patientSex"
                    label="性别"
                    width="100">
                </el-table-column>
                <el-table-column
                    prop="patientAge"
                    label="年龄"
                    width="100">
                </el-table-column>
                <el-table-column
                    prop="patientPhone"
                    label="手机号"
                    width="160">
                </el-table-column>
                <el-table-column
                    prop="patientIdCard"
                    label="身份证号">
                </el-table-column>
                <el-table-column
                    label="操作"
                    width="100">
                    <template slot-scope="scope">
                        <el-button  @click="examBtn(scope.row)" type="text" size="small">体检小结</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <hr class="hr-solid-content" data-content="套餐">   
            <el-table
                :data="tableData2"
                border
                style="width: 100%;">
                <el-table-column
                    prop="department"
                    label="所属科室"
                    width="80">
                </el-table-column>
                <el-table-column
                    prop="comName"
                    label="套餐名"
                    width="140">
                </el-table-column>
                <el-table-column
                    prop="proName"
                    label="项目名"
                    width="140">
                </el-table-column>
                <el-table-column
                    prop="patientName"
                    label="姓名"
                    width="110">
                </el-table-column>
                <el-table-column
                    prop="patientSex"
                    label="性别"
                    width="70">
                </el-table-column>
                <el-table-column
                    prop="patientAge"
                    label="年龄"
                    width="70">
                </el-table-column>
                <el-table-column
                    prop="patientPhone"
                    label="手机号"
                    width="150">
                </el-table-column>
                <el-table-column
                    prop="patientIdCard"
                    label="身份证号"
                    width="170">
                </el-table-column>
                <el-table-column
                    label="操作"
                    >
                    <template slot-scope="scope">
                        <el-button @click="examBtn(scope.row)" type="text" size="small">体检小结</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-dialog
                :visible.sync="dialogFormVisible"
                title="体检小结"
                style="font-size: 16px;"
                >
                <el-form ref="form" :model="form" :rules="rules">
                    <el-form-item label="项目名:" :label-width="formLabelWidth">
                        <div style="text-align: left;">{{ form.projectName }}</div>
                    </el-form-item>
                    <el-table :data="tableData2Child" style="padding:0px 25px;margin-top:-20%" border>
                        <el-table-column property="detailsName" label="细项名称" width="150"></el-table-column>
                        <el-table-column property="detailsUnit" label="细项单位" width="150"></el-table-column>
                        <el-table-column property="detailsUp" label="上限值" width="120"></el-table-column>
                        <el-table-column property="detailsDown" label="下限值"></el-table-column>

                    </el-table>
                    <el-form-item style="margin-top: 10px" label="体检小结" prop="desc" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="form.desc"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit(form)">提交</el-button>
                        <el-button @click="cancel">取消</el-button>
                    </el-form-item>
                </el-form>
            </el-dialog>
        </div>
    </div>
</template>
  
<script>
export default {
    data() {
        return {
            input: '',
            select: '',
            //项目表
            tableData1:[],
             //套餐表
            tableData2:[],
            dialogFormVisible:false,
            form:{
                desc: '',
                projectName:'',
            },
            formLabelWidth: '100px',
            tableData2Child: [],
            rules:{
                desc: [
                    { required: true, message: '请填写体检小结', trigger: 'blur' }
                ]
            },
        }
    },

    methods:{
        async searchBtn(){
            // 每次搜索都将数组置零
            this.tableData1 = [];
            this.tableData2 = [];
            if(this.select == 0 || this.select == null){
                //判断一下，要不然会报错
                alert("选择类型不能为空")
            }else{
                //到后端发送请求
                this.$axios.get(
                    "/order/getInfo",
                    {
                        params:{
                            id:this.input,
                            sort:this.select
                        }
                    }
                ).then(res=>{
                    console.log("111",res)
                    if(res != undefined){
                        console.log(res)
                        if(res.data.length == 0){
                            this.$message.error("暂无该用户")
                        }else{
                            var patientInfo = res.data[0].cyPatient //暂时存一下,要不然到后面无法使用哦
                        //接下来要做的事情就是将对应的项目或者是套餐获取到对应的科室
                            for(var i = 0 ; i< res.data.length;i++){
                                //如果是1则说明这是项目--》状态status为1，发送请求到后端求得科室名称
                                if(res.data[i].orderProOrCom == 1){
                                    //先分割出来，拿到每个项目名
                                    var info = res.data[i].orderInfo.split(",")
                                    // 发送请求
                                    for(var j = 0 ; j < info.length;j++){
                                        this.getDepartName1(patientInfo,info[j])
                                    }
                                // proName.push(info)
                                }else{
                                    // 如果是2则说明这是项目--》状态status为2，发送请求到后端求得科室名称
                                    var info = res.data[i].orderInfo.split(",")
                                    // 跟上面同理
                                    for(var j = 0 ; j < info.length;j++){
                                        // 弄一个临时变量来存储一下套餐名
                                        this.getDepartName2(patientInfo,info[j])
                                    }
                                }
                            //做到这里就可以获取到用户所需要的科室
                            //医生就要检索到是否为本科室的体检病人，那么我们就在上面代码继续判断跟登录的体检医生是否是同一个科室
                            }
                        }
                        
                    }
                })
                console.log("项目表",this.tableData1)
                console.log("套餐表",this.tableData2)
            }
        },
        async getDepartName1(patientInfo,info){
            await this.$axios.get(
                "/order/getDepartName",{
                    params:{
                        status:1,
                        name:info
                    }
                }
            ).then(res=>{
                // 判断用户的体检科室是否跟体检医生的一致
                if(res != undefined){
                    if(res.data == this.$store.state.departName){
                        // 如果有相同的那就保存到tableData里面进行存储
                        // 定义一个对象
                        let data = {
                            proName:info,
                            patientName:patientInfo.patientName,
                            patientAge:patientInfo.patientAge,
                            patientSex:patientInfo.patientSex,
                            patientPhone:patientInfo.patientPhone,
                            patientIdCard:patientInfo.patientIdCard,
                            department:res.data
                        }
                        // 将对象的值传入数组中
                        this.tableData1.push(data)
                    }
                }
                
            })
        },
        async getDepartName2(patientInfo,info){
            await this.$axios.get(
                "/order/getDepartName",{
                    params:{
                        status:2,
                        name:info
                    }
                }).then(res=>{
                    // 返回 科室-项目&科室-项目
                    var temp = res.data.split('&');
                    for(var z = 0; z< temp.length;z++){
                        // 分隔出来就是 科室-项目
                        // 再次分隔出来到 科室 项目
                        var tm = temp[z].split("-") 
                        if(tm[0] == this.$store.state.departName){
                            // 说明是同一个科室的
                            // 定义一个对象
                            let data = {
                                comName:info,
                                proName:tm[1],
                                patientName:patientInfo.patientName,
                                patientAge:patientInfo.patientAge,
                                patientSex:patientInfo.patientSex,
                                patientPhone: patientInfo.patientPhone,
                                patientIdCard:patientInfo.patientIdCard,
                                department:tm[0] 
                            }
                            this.tableData2.push(data)
                        }
                    }
                })
        },
        examBtn(row){
            this.form.projectName = row.proName
            this.$confirm('确定用户的信息无误嘛？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.selectDetail(row.proName)
                this.selectSummary(row.proName)
                this.dialogFormVisible = true
            }).catch(() => {
                    this.$message({
                    type: 'info',
                    message: '已取消操作'
                });          
            });
      
        },
        onSubmit(formName){
            if(formName.desc == ""){
                this.$message.error("必须填写的信息不能为空")
            }else{
                this.$axios.get(
                    "/visitDoctor/perfectInfo",
                    {
                        params:{
                            adminId:this.$store.state.adminId,
                            summary:formName.desc,
                            proName:this.form.projectName
                        }
                    }
                ).then(res=>{
                    if(res != undefined){
                        this.$notify({
                            title: '体检小结',
                            message: '体检小结写入成功',
                            type: 'success'
                        });
                    }
                    
                })
                this.dialogFormVisible = false
            }
        },
        selectDetail(name){
            this.$axios.get(
                "/project/selectProjectByDetails",{
                    params:{
                        name:name
                    }
                }
            ).then(res=>{
                if(res != undefined){
                    console.log("查询所对应的细项",res.data)
                    this.tableData2Child = res.data
                } 
            })
        },
        selectSummary(name){
            this.$axios.get(
                "/visitDoctor/selectSummary",
                {
                    params:{
                        name:name
                    }
                }
            ).then(res=>{
                console.log(res)
                if(res.code == 1){
                    this.form.desc = res.data
                }
            })
        },
        cancel(){
            this.dialogFormVisible = false;
            this.$message({
                showClose: true,
                message: '已经取消写入'
            });
        }
    }
}
</script>
<style>
.el-select .el-input {
    width: 130px;
}
.input-with-select .el-input-group__prepend {
    background-color: #fff;
}
.hr-solid-content{
    color: #4e4e4f;
    font-family: 'STHupo', sans-serif;
    border: 0;
    font-size: 20px;
    padding: 1em 0;
    position: relative;
}
.hr-solid-content::before {
    content: attr(data-content);
    position: absolute;
    padding: 0 1ch;
    line-height: 1px;
    border: solid #d0d0d5;
    border-width: 0 99vw;
    width: fit-content;
    /* for IE浏览器 */
    white-space: nowrap;
    left: 50%;
    transform: translateX(-50%);
}
.note{
    float: left;
    color: rgb(104, 103, 103);
    margin-top: -1%;
}
.note p{
    margin-top: -15px;
    font-size: 14px;
    font-family: 'PingFang SC', sans-serif;
}
.el-table .cell {  
  text-align: left;  
}  
.el-form-item__label {
    font-size: 16px;
}
</style>